<!DOCTYPE html>
<html lang="en"><head>
    <title>CZH-DEV BLOG</title>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="format-detection" content="telephone=no" />
    <meta name="theme-color" content="#000084" />
    <link rel="icon" href="https://czh-dev.gitee.io/czh-blog.gitee.io//favicon.ico">
    <link rel="canonical" href="https://czh-dev.gitee.io/czh-blog.gitee.io/">
    
    
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="navbar-inner">
        <div class="container">
            <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"></button>
            <a class="brand" href="https://czh-dev.gitee.io/czh-blog.gitee.io/">CZH-DEV BLOG</a>
            <div class="nav-collapse collapse">
                <ul class="nav">
                    
                    
                        
                            <li>
                                <a href="/czh-blog.gitee.io/about/">
                                    
                                    <span>About</span>
                                </a>
                            </li>
                        
                    
                        
                            <li>
                                <a href="/czh-blog.gitee.io/post/">
                                    
                                    <span>All posts</span>
                                </a>
                            </li>
                        
                    
                        
                            <li>
                                <a href="/czh-blog.gitee.io/ebook/">
                                    
                                    <span>Resource</span>
                                </a>
                            </li>
                        
                    
                </ul>
            </div>
        </div>
    </div>
</nav><div id="content" class="container">
<div style="display: flex;">
  <div class="row-fluid navmargin">
    <div class="page-header">
      <h1>MongoDB - Fri, Feb 10, 2023</h1>
    </div>
    <p class="lead"></p>
    <h1 id="mongodb">MongoDB</h1>
<p><img src="https://czh-pic.oss-cn-guangzhou.aliyuncs.com/202302110107791.png" alt="image-20221231215718588"></p>
<h2 id="mongodb介绍">MongoDB介绍</h2>
<h3 id="简介">简介</h3>
<p><strong>MongoDB</strong>是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。</p>
<p><strong>MongoD</strong>B是一个介于关系数据库和非关系数据库之间的产品，是非关系数据库当中功能最丰富，最像关系数据库的。它支持的数据结构非常松散，是类似json的bson格式，因此可以存储比较复杂的数据类型。Mongo最大的特点是它支持的查询语言非常强大，其语法有点类似于面向对象的查询语言，几乎可以实现类似关系数据库单表查询的绝大部分功能，而且还支持对数据建立索引。</p>
<p><a href="https://baijiahao.baidu.com/s?id=1628146397605671081&amp;wfr=spider&amp;for=pc">2019 数据库最受欢迎排行榜 (baidu.com)</a></p>
<h3 id="特点">特点</h3>
<ul>
<li>高性能</li>
<li>易部署</li>
<li>易使用</li>
<li>非常方便的存储数据</li>
</ul>
<h3 id="对比mysql">对比MySQL</h3>
<table>
<thead>
<tr>
<th></th>
<th>MongoDB</th>
<th>Mysql</th>
</tr>
</thead>
<tbody>
<tr>
<td>数据库模型</td>
<td>非关系型</td>
<td>关系型</td>
</tr>
<tr>
<td>存储方式</td>
<td>虚拟内存 + 持久化</td>
<td>不同存储引擎有不同的存储方式</td>
</tr>
<tr>
<td>查询语句</td>
<td>独特的MongoDB查询方式</td>
<td>传统SQL语句</td>
</tr>
<tr>
<td>架构特点</td>
<td>可以通过副本集，以及分片来实现高可用</td>
<td>常见也有单点，M-S，MHA，MMM，Cluster等架构</td>
</tr>
<tr>
<td>数据处理方式</td>
<td>基于内存，将热数据存在物理内存中，从而达到高速读写</td>
<td>不同的引擎拥有其自己的特点</td>
</tr>
<tr>
<td>成熟度</td>
<td>新兴数据库，成熟度低</td>
<td>拥有较为成熟的体系，成熟度较高</td>
</tr>
<tr>
<td>广泛度</td>
<td>NoSQL数据库中，MongoDB是较为完善的DB之一，使用人群也在不断增长</td>
<td>开源数据库的份额在不断增加，MySQL的份额也在不断增长</td>
</tr>
</tbody>
</table>
<p>目前环境下，只要对事务要求不高的业务都能被MongoDB所取代，属于及其热门的NoSQL数据库</p>
<h3 id="数据库结构">数据库结构</h3>
<p>MongoDB属于NoSQL数据库，自然也是没有表相关的概念的，该数据库存储使用的是集合，集合中存储的是文档（树状结构数据）</p>
<p><img src="https://czh-pic.oss-cn-guangzhou.aliyuncs.com/202302110108840.png" alt="image-20221231235348466"></p>
<h3 id="语法">语法</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span><span style="color:#66d9ef">show</span> dbs <span style="color:#75715e">--查询所有数据库
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>use <span style="color:#960050;background-color:#1e0010">数据库名</span> <span style="color:#75715e">--创建并且选中数据库，数据库已经存在则直接选中
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>db <span style="color:#75715e">--查询当前选择的数据库
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>db.dropDatabase() <span style="color:#75715e">--删除当前选中的数据库
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">show</span> collections <span style="color:#75715e">--查询当前库中的集合
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>db.createCollection(<span style="color:#e6db74">&#34;集合名&#34;</span>) <span style="color:#75715e">--创建集合
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.<span style="color:#66d9ef">drop</span>() <span style="color:#75715e">--删除集合
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#960050;background-color:#1e0010">注意：</span>
</span></span><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span> <span style="color:#f92672">==</span> db.getCollection(<span style="color:#e6db74">&#34;集合名&#34;</span>)
</span></span></code></pre></div><h3 id="数据类型">数据类型</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yaml" data-lang="yaml"><span style="display:flex;"><span><span style="color:#f92672">String（字符串）</span>: <span style="color:#ae81ff">mongodb中的字符串是UTF-8有效的</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">Integer（整数）</span>: <span style="color:#ae81ff">存储数值。整数可以是32位或64位，具体取决于您的服务器</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">Boolean（布尔）</span>: <span style="color:#ae81ff">存储布尔(true/false)值</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">Double（双精度）</span>: <span style="color:#ae81ff">存储浮点值</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">Arrays（数组）</span>: <span style="color:#ae81ff">将数组或列表或多个值存储到⼀个键中</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">Timestamp（时间戳）</span>: <span style="color:#ae81ff">存储时间戳</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">Object（对象）</span>: <span style="color:#ae81ff">嵌⼊式⽂档</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">Null （空值）</span>: <span style="color:#ae81ff">存储Null值</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">Symbol（符号）</span>: <span style="color:#ae81ff">与字符串相同，⽤于具有特定符号类型的语⾔</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">Date（⽇期）</span>: <span style="color:#ae81ff">以UNIX时间格式存储当前⽇期或时间</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">Object ID（对象ID） </span>: <span style="color:#ae81ff">存储⽂档ID</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">Binary data（⼆进制数据）</span>: <span style="color:#ae81ff">存储⼆进制数据</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">Code（代码）</span>: <span style="color:#ae81ff">将JavaScript代码存储到⽂档中</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">Regular expression（正则表达式）</span>: <span style="color:#ae81ff">存储正则表达式</span>
</span></span></code></pre></div><h2 id="安装和连接">安装和连接</h2>
<p>懒得重新安一遍，直接找个安装教程</p>
<p><a href="https://www.cnblogs.com/zhoulifeng/p/9429597.html">MongoDB安装配置教程 - 小周sri的码农 - 博客园 (cnblogs.com)</a></p>
<h2 id="创建操作">创建操作</h2>
<p>创建或插入操作会将新文档添加到集合中，如果该集合当前不存在，则插入操作将创建该集合</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.<span style="color:#66d9ef">insert</span>();
</span></span><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.insertOne();
</span></span><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.insertMany([]);
</span></span></code></pre></div><p><img src="https://czh-pic.oss-cn-guangzhou.aliyuncs.com/202302110108900.png" alt="image-20221231230529335"></p>
<ul>
<li><!-- raw HTML omitted --><code>db.集合名.insertOne(...)</code><!-- raw HTML omitted -->3.2版本中的新功能</li>
<li><!-- raw HTML omitted --><code>db.集合名.insertMany(...)</code><!-- raw HTML omitted -->3.2版本中的新功能</li>
</ul>
<p>当操作成功时，集合会给文档生成一个**_id**字段，该字段就是文档的主键，也能在插入数据时自己指定该字段的值，但是不建议这样做</p>
<p>例：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.users.<span style="color:#66d9ef">insert</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">{</span>
</span></span><span style="display:flex;"><span>        id:<span style="color:#ae81ff">1</span>, 
</span></span><span style="display:flex;"><span>        name:<span style="color:#e6db74">&#34;czh&#34;</span>, 
</span></span><span style="display:flex;"><span>        age:<span style="color:#ae81ff">21</span>
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">}</span>
</span></span><span style="display:flex;"><span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>db.users.insertmany(
</span></span><span style="display:flex;"><span>    [
</span></span><span style="display:flex;"><span>        <span style="color:#960050;background-color:#1e0010">{</span>id:<span style="color:#ae81ff">3</span>, name:<span style="color:#e6db74">&#34;ly&#34;</span>, age:<span style="color:#ae81ff">23</span><span style="color:#960050;background-color:#1e0010">}</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#960050;background-color:#1e0010">{</span>id:<span style="color:#ae81ff">4</span>, name:<span style="color:#e6db74">&#34;wgq&#34;</span>, age:<span style="color:#ae81ff">21</span><span style="color:#960050;background-color:#1e0010">}</span>
</span></span><span style="display:flex;"><span>    ]
</span></span><span style="display:flex;"><span>);
</span></span></code></pre></div><h2 id="删除操作">删除操作</h2>
<p>删除操作从集合中删除文档。MongDB提供一下删除集合文档的方法：</p>
<p><img src="https://czh-pic.oss-cn-guangzhou.aliyuncs.com/202302110108603.png" alt="image-20221231235229832"></p>
<ul>
<li><!-- raw HTML omitted --><code>db.collection.deleteOne(...)</code><!-- raw HTML omitted -->3.2版中的新功能</li>
<li><!-- raw HTML omitted --><code>db.collection.deleteMany(...)</code><!-- raw HTML omitted -->3.2版中的新功能</li>
</ul>
<p>在MongoDB中，删除操作的目标是单个<!-- raw HTML omitted -->collection<!-- raw HTML omitted -->。MongoDB中的所有写操作都是单个文档级别的<strong>原子操作</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.remove(
</span></span><span style="display:flex;"><span>	<span style="color:#f92672">&lt;</span>query<span style="color:#f92672">&gt;</span><span style="color:#960050;background-color:#1e0010">，</span>
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">{</span>
</span></span><span style="display:flex;"><span>        justOne: <span style="color:#f92672">&lt;</span>boolean<span style="color:#f92672">&gt;</span><span style="color:#960050;background-color:#1e0010">，</span>
</span></span><span style="display:flex;"><span>        writeConcern: <span style="color:#f92672">&lt;</span>document<span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">}</span>
</span></span><span style="display:flex;"><span>)
</span></span></code></pre></div><ul>
<li><strong>query</strong> :（可选）删除的文档的条件</li>
<li><strong>justOne</strong> : （可选）如果设为 true 或 1，则只删除一个文档，如果不设置该参数，或使用默认值false，则删除所有匹配条件的文档</li>
<li><strong>writeConcern</strong> :（可选）抛出异常的级别</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.<span style="color:#66d9ef">delete</span>();
</span></span><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.deleteOne();
</span></span><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.deleteMany(<span style="color:#960050;background-color:#1e0010">{}</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.remove()
</span></span></code></pre></div><blockquote>
<p><code>注意：</code></p>
<ol>
<li>
<p><strong>deleteOne()</strong> &ndash; 删除第一条满足条件的数据</p>
</li>
<li>
<p><strong>remove()</strong> &ndash; 删除所有满足条件的数据，和**deleteMany()**等价</p>
</li>
<li>
<p>要使用自动生成的**_id**进行删除时，需要封装成Object类</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.users.deleteOne(<span style="color:#960050;background-color:#1e0010">{</span>_id:ObjectId(<span style="color:#e6db74">&#34;6224308aa569000058005895&#34;</span>)<span style="color:#960050;background-color:#1e0010">}</span>);
</span></span></code></pre></div></li>
</ol>
</blockquote>
<p>例：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.users.deleteMany(<span style="color:#960050;background-color:#1e0010">{}</span>); <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">传个空删除全部记录</span>
</span></span><span style="display:flex;"><span>db.users.deleteMant(<span style="color:#960050;background-color:#1e0010">{</span>age:<span style="color:#ae81ff">23</span><span style="color:#960050;background-color:#1e0010">}</span>); <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">删除年龄为</span><span style="color:#ae81ff">23</span><span style="color:#960050;background-color:#1e0010">的用户</span>
</span></span><span style="display:flex;"><span>db.users.remove(<span style="color:#960050;background-color:#1e0010">{</span>age: <span style="color:#ae81ff">23</span><span style="color:#960050;background-color:#1e0010">}</span>) <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">删除所有年龄为</span><span style="color:#ae81ff">23</span><span style="color:#960050;background-color:#1e0010">的用户</span>
</span></span></code></pre></div><h2 id="更新操作">更新操作</h2>
<p>修改集合中已经存在的文档。MongDB提供了一下方法来更新集合的文档：</p>
<p><img src="https://czh-pic.oss-cn-guangzhou.aliyuncs.com/202302110108737.png" alt="image-20221231234425047"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.<span style="color:#66d9ef">update</span>(
</span></span><span style="display:flex;"><span>	<span style="color:#f92672">&lt;</span>query<span style="color:#f92672">&gt;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;</span><span style="color:#66d9ef">update</span><span style="color:#f92672">&gt;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">{</span>
</span></span><span style="display:flex;"><span>    	upsert: <span style="color:#f92672">&lt;</span>boolean<span style="color:#f92672">&gt;</span>,
</span></span><span style="display:flex;"><span>    	multi: <span style="color:#f92672">&lt;</span>boolean<span style="color:#f92672">&gt;</span>,
</span></span><span style="display:flex;"><span>    	writeConcern: <span style="color:#f92672">&lt;</span>document<span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">}</span>
</span></span><span style="display:flex;"><span>);
</span></span></code></pre></div><p><strong>参数说明：</strong></p>
<ul>
<li><strong>query</strong> : update的查询条件，类似sql update查询内where后面的</li>
<li><strong>update</strong> : update的对象和一些更新的操作符（如$，$inc&hellip;）等，也可以理解为sql update查询内set后面的</li>
<li><strong>upsert</strong> : 可选，这个参数的意思是，如果不存在update的记录，是否插入objNew，true为插入，默认是false，不插入。</li>
<li><strong>multi</strong> : 可选，mongodb 默认是false，只更新找到的第一条记录，如果这个参数为true，就把按条件查出来多条记录全部更新</li>
<li><strong>writeConcern</strong> :可选，抛出异常的级别</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.<span style="color:#66d9ef">update</span>(...);
</span></span><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.updateOne(...);
</span></span><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.updateMany(...);
</span></span></code></pre></div><blockquote>
<p><code>注意：</code></p>
<p>把**$set<strong>改成</strong>$unset**,在把字段设为空值，<strong>表示删除该字段</strong>，而不是修改该字段为空值</p>
</blockquote>
<p>例：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.users.<span style="color:#66d9ef">update</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">{</span>age:<span style="color:#ae81ff">21</span><span style="color:#960050;background-color:#1e0010">}</span>, 
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">{$</span><span style="color:#66d9ef">set</span><span style="color:#960050;background-color:#1e0010">{</span>sex:<span style="color:#e6db74">&#34;男&#34;</span><span style="color:#960050;background-color:#1e0010">}}</span>
</span></span><span style="display:flex;"><span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>db.users.<span style="color:#66d9ef">update</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">{{</span>age:<span style="color:#ae81ff">21</span><span style="color:#960050;background-color:#1e0010">}</span>, 
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">${</span>unset<span style="color:#960050;background-color:#1e0010">{</span>sex:<span style="color:#e6db74">&#34;&#34;</span><span style="color:#960050;background-color:#1e0010">}}</span> <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">删除</span>sex字段
</span></span><span style="display:flex;"><span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>db.users.updateOne(
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">{</span>name:<span style="color:#e6db74">&#34;czh&#34;</span><span style="color:#960050;background-color:#1e0010">}</span>, 
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">{$</span><span style="color:#66d9ef">set</span>:<span style="color:#960050;background-color:#1e0010">{</span>name:<span style="color:#e6db74">&#34;lenovo&#34;</span><span style="color:#960050;background-color:#1e0010">}}</span>
</span></span><span style="display:flex;"><span>);
</span></span></code></pre></div><h2 id="读取操作">读取操作</h2>
<p>读取操作从 集合中检索文档; 即查询集合以获取文档。MongoDB提供了以下方法来从集合中读取文档：</p>
<p><img src="https://czh-pic.oss-cn-guangzhou.aliyuncs.com/202302110108799.png" alt="image-20221231232745048"></p>
<h3 id="正常查询">正常查询</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.find(<span style="color:#960050;background-color:#1e0010">{}</span>,<span style="color:#960050;background-color:#1e0010">{}</span>);
</span></span></code></pre></div><blockquote>
<p><code>注意：</code></p>
<p>第一个**{ }<strong>，中填条件，第二个</strong>{ }**填显示的列 &ndash;<strong>1</strong>（显示），<strong>0</strong>（不显示）</p>
</blockquote>
<h3 id="分页查询">分页查询</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.find().skip().<span style="color:#66d9ef">limit</span>();
</span></span></code></pre></div><blockquote>
<p><code>注意：</code></p>
<p><strong>skip</strong>相当于MySQL中分页查询的**（currentPage-1）<em>pageSize</em>*</p>
<p><strong>limit</strong>相当于MySQL中的<strong>每页查询几条数据</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.users.find().skip(<span style="color:#ae81ff">0</span>).<span style="color:#66d9ef">limit</span>(<span style="color:#ae81ff">3</span>); <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">第一页</span>
</span></span><span style="display:flex;"><span>db.users.find().skip(<span style="color:#ae81ff">3</span>).<span style="color:#66d9ef">limit</span>(<span style="color:#ae81ff">3</span>); <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">第二页</span>
</span></span><span style="display:flex;"><span>db.users.find().skip(<span style="color:#ae81ff">6</span>).<span style="color:#66d9ef">limit</span>(<span style="color:#ae81ff">3</span>); <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">第三页</span>
</span></span></code></pre></div></blockquote>
<p>例：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.users.find(); <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">查询全部</span>
</span></span><span style="display:flex;"><span>db.users.find(<span style="color:#960050;background-color:#1e0010">{</span>name:<span style="color:#e6db74">&#34;czh&#34;</span>, age:<span style="color:#ae81ff">21</span><span style="color:#960050;background-color:#1e0010">}</span>);
</span></span></code></pre></div><h3 id="排序">排序</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.find().sort();
</span></span></code></pre></div><blockquote>
<p><code>注意：</code></p>
<p><strong>sort</strong>中的参数<strong>1</strong>为升序，<strong>-1</strong>为降序</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.users.find().sort(<span style="color:#960050;background-color:#1e0010">{</span>age:<span style="color:#ae81ff">1</span>, id:<span style="color:#f92672">-</span><span style="color:#ae81ff">1</span><span style="color:#960050;background-color:#1e0010">}</span>)
</span></span></code></pre></div></blockquote>
<h3 id="高级查询">高级查询</h3>
<h4 id="比较查询">比较查询</h4>
<p><strong>比较操作符</strong></p>
<ul>
<li><code>&gt;</code>大于 &ndash; <code>$gt</code></li>
<li><code>&lt;</code>小于 &ndash; <code>$lt</code></li>
<li><code>&gt;=</code>大于等于 &ndash; <code>$gte</code></li>
<li><code>&lt;=</code>小于等于 &ndash; <code>$lte</code></li>
<li><code>!=</code>不等于 &ndash; <code>$ne</code></li>
<li>集合运算符 &ndash; <code>$in</code>,如：<code>{name: {$in: [&quot;czh&quot;, &quot;ly&quot;]}}</code></li>
<li>判断存在 &ndash; <code>$exists</code>,如：<code>{name: {$exists:true}}</code></li>
</ul>
<p><strong>大于/大于等于</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.find(<span style="color:#960050;background-color:#1e0010">{字段名</span>:<span style="color:#960050;background-color:#1e0010">{$</span>gt:<span style="color:#960050;background-color:#1e0010">数字}}</span>); <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">大于</span>
</span></span><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.find(<span style="color:#960050;background-color:#1e0010">{字段名</span>:<span style="color:#960050;background-color:#1e0010">{$</span>gte:<span style="color:#960050;background-color:#1e0010">数字}}</span>); <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">大于等于</span>
</span></span></code></pre></div><p>例：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.users.find(<span style="color:#960050;background-color:#1e0010">{</span>age:<span style="color:#960050;background-color:#1e0010">{$</span>gt:<span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">}}</span>) <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">查询年龄大于</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">的用户</span>
</span></span><span style="display:flex;"><span>db.users.find(<span style="color:#960050;background-color:#1e0010">{</span>age:<span style="color:#960050;background-color:#1e0010">{$</span>gte:<span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">}}</span>) <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">查询年龄大于等于</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">的用户</span>
</span></span></code></pre></div><p><strong>小于/小于等于</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.find(<span style="color:#960050;background-color:#1e0010">{字段名</span>:<span style="color:#960050;background-color:#1e0010">{$</span>lt:<span style="color:#960050;background-color:#1e0010">数字}}</span>); <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">小于</span>
</span></span><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.find(<span style="color:#960050;background-color:#1e0010">{字段名</span>:<span style="color:#960050;background-color:#1e0010">{$</span>lte:<span style="color:#960050;background-color:#1e0010">数字}}</span>); <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">小于等于</span>
</span></span></code></pre></div><p>例：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.users.find(<span style="color:#960050;background-color:#1e0010">{</span>age:<span style="color:#960050;background-color:#1e0010">{$</span>lt:<span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">}}</span>) <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">查询年龄小于</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">的用户</span>
</span></span><span style="display:flex;"><span>db.users.find(<span style="color:#960050;background-color:#1e0010">{</span>age:<span style="color:#960050;background-color:#1e0010">{$</span>lte:<span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">}}</span>) <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">查询年龄小于等于</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">的用户</span>
</span></span></code></pre></div><p><strong>不等于</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.find(<span style="color:#960050;background-color:#1e0010">{字段名</span>:<span style="color:#960050;background-color:#1e0010">{$</span>ne:<span style="color:#960050;background-color:#1e0010">数字}}</span>); <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">不等于</span>
</span></span></code></pre></div><p>例：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.users.find(<span style="color:#960050;background-color:#1e0010">{</span>age:<span style="color:#960050;background-color:#1e0010">{$</span>ne:<span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">}}</span>) <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">查询年龄不等于</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">的用户</span>
</span></span></code></pre></div><p><strong>集合运算：in</strong></p>
<p>和MySQL中的<strong>in</strong>效果一样</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.<span style="color:#960050;background-color:#1e0010">集合名</span>.find(<span style="color:#960050;background-color:#1e0010">{字段名</span>:<span style="color:#960050;background-color:#1e0010">{$</span><span style="color:#66d9ef">in</span>:[<span style="color:#960050;background-color:#1e0010">元素</span><span style="color:#ae81ff">1</span>, <span style="color:#960050;background-color:#1e0010">元素</span><span style="color:#ae81ff">2</span> ...]<span style="color:#960050;background-color:#1e0010">}}</span>); <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">查询在这个集合中的所有元素</span>
</span></span></code></pre></div><p>例：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.users.find(<span style="color:#960050;background-color:#1e0010">{</span>name:<span style="color:#960050;background-color:#1e0010">{$</span><span style="color:#66d9ef">in</span>:[<span style="color:#e6db74">&#34;czh&#34;</span>, <span style="color:#e6db74">&#34;ly&#34;</span>]<span style="color:#960050;background-color:#1e0010">}}</span>)
</span></span></code></pre></div><h4 id="逻辑查询">逻辑查询</h4>
<ul>
<li><code>&amp;&amp;</code>与 &ndash; <code>$and</code></li>
<li><code>||</code>或 &ndash; <code>$or</code></li>
<li><code>!</code>非 &ndash; <code>$not</code></li>
</ul>
<p><strong>$and</strong></p>
<p>普通sql语句：<code>select * from users where age &gt;= 20 and age &lt;= 30</code></p>
<p>比较查询：<code>db.users.find({age:{\$gte:20, $lte:30}})</code></p>
<p>逻辑查询：<code>db.users.find({$and:[{age:{$gte:20}}, {age:{$lte:30}}]})</code></p>
<p><strong>$or</strong></p>
<p>语法与and相同</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.users.find(<span style="color:#960050;background-color:#1e0010">{$</span><span style="color:#66d9ef">or</span>:[<span style="color:#960050;background-color:#1e0010">{</span>age:<span style="color:#960050;background-color:#1e0010">{$</span>gte:<span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">}}</span>, <span style="color:#960050;background-color:#1e0010">{</span>age:<span style="color:#960050;background-color:#1e0010">{$</span>lte:<span style="color:#ae81ff">30</span><span style="color:#960050;background-color:#1e0010">}}</span>]<span style="color:#960050;background-color:#1e0010">}</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>db.users.find(<span style="color:#960050;background-color:#1e0010">{$</span><span style="color:#66d9ef">or</span>:[<span style="color:#960050;background-color:#1e0010">{</span>age:<span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">}</span>, <span style="color:#960050;background-color:#1e0010">{</span>age:<span style="color:#ae81ff">21</span><span style="color:#960050;background-color:#1e0010">}</span>]<span style="color:#960050;background-color:#1e0010">}</span>)
</span></span></code></pre></div><h4 id="模糊查询">模糊查询</h4>
<p><strong>对应SQL语句：</strong></p>
<ul>
<li>
<p><code>regex</code> &mdash;&gt; <code>like</code></p>
</li>
<li>
<p><code>{name:/xx/} </code>&ndash;&gt; <code>%xx%</code></p>
</li>
<li>
<p><code>{name:/^xx/}</code> &mdash;&gt; <code>xx%</code></p>
</li>
<li>
<p><code>{name:/xx$/}</code> &mdash;&gt; <code>%xx</code></p>
</li>
<li>
<p><code>{name:/xx/i}</code> &mdash;&gt; <code>忽略大小写</code></p>
</li>
</ul>
<p>例：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.users.find(<span style="color:#960050;background-color:#1e0010">{</span>name:<span style="color:#960050;background-color:#1e0010">{$</span>regex:<span style="color:#f92672">/</span>z<span style="color:#f92672">/</span><span style="color:#960050;background-color:#1e0010">}}</span>)
</span></span><span style="display:flex;"><span>db.users.find(<span style="color:#960050;background-color:#1e0010">{</span>name:<span style="color:#960050;background-color:#1e0010">{$</span>regex:<span style="color:#f92672">/^</span>cz<span style="color:#f92672">/</span><span style="color:#960050;background-color:#1e0010">}}</span>)
</span></span><span style="display:flex;"><span>db.users.find(<span style="color:#960050;background-color:#1e0010">{</span>name:<span style="color:#960050;background-color:#1e0010">{$</span>regex:<span style="color:#f92672">/</span>zh$<span style="color:#f92672">/</span><span style="color:#960050;background-color:#1e0010">}}</span>)
</span></span><span style="display:flex;"><span>db.users.find(<span style="color:#960050;background-color:#1e0010">{</span>name:<span style="color:#960050;background-color:#1e0010">{$</span>regex:<span style="color:#f92672">/</span>w<span style="color:#f92672">/</span>i<span style="color:#960050;background-color:#1e0010">}}</span>);
</span></span></code></pre></div><p><strong>练习：查询名字中带有”治“，并且年龄在20到30岁之间的用户</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.users.find(<span style="color:#960050;background-color:#1e0010">{$</span><span style="color:#66d9ef">and</span>:[<span style="color:#960050;background-color:#1e0010">{</span>name:<span style="color:#960050;background-color:#1e0010">{$</span>regex:<span style="color:#f92672">/</span><span style="color:#960050;background-color:#1e0010">治</span><span style="color:#f92672">/</span><span style="color:#960050;background-color:#1e0010">}}</span>, <span style="color:#960050;background-color:#1e0010">{</span>age:<span style="color:#960050;background-color:#1e0010">{$</span>gte:<span style="color:#ae81ff">20</span>, <span style="color:#960050;background-color:#1e0010">$</span>lte:<span style="color:#ae81ff">30</span><span style="color:#960050;background-color:#1e0010">}}</span>]<span style="color:#960050;background-color:#1e0010">}</span>)
</span></span></code></pre></div><h3 id="数组----push">数组 &ndash; $push</h3>
<h4 id="添加元素">添加元素</h4>
<blockquote>
<p><code>$push</code> &ndash;&gt; 可以重复添加</p>
<p><code>$addToSet</code> &ndash;&gt; 不重复</p>
</blockquote>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.users.<span style="color:#66d9ef">update</span>(<span style="color:#960050;background-color:#1e0010">{</span>name:<span style="color:#e6db74">&#34;qwe&#34;</span><span style="color:#960050;background-color:#1e0010">}</span>, <span style="color:#960050;background-color:#1e0010">{$</span>push:<span style="color:#960050;background-color:#1e0010">{</span>hobby:<span style="color:#e6db74">&#34;java&#34;</span><span style="color:#960050;background-color:#1e0010">}}</span>) <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">添加一个元素</span>
</span></span><span style="display:flex;"><span>db.users.<span style="color:#66d9ef">update</span>(<span style="color:#960050;background-color:#1e0010">{</span>name:<span style="color:#e6db74">&#34;qwe&#34;</span><span style="color:#960050;background-color:#1e0010">}</span>, <span style="color:#960050;background-color:#1e0010">{$</span>push:<span style="color:#960050;background-color:#1e0010">{</span>hobby:<span style="color:#960050;background-color:#1e0010">{$</span><span style="color:#66d9ef">each</span>:[<span style="color:#e6db74">&#34;vue&#34;</span>, <span style="color:#e6db74">&#34;c&#34;</span>, <span style="color:#e6db74">&#34;c++&#34;</span>]<span style="color:#960050;background-color:#1e0010">}}}</span>) <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">循环添加多个元素</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>db.users.<span style="color:#66d9ef">update</span>(<span style="color:#960050;background-color:#1e0010">{</span>name:<span style="color:#e6db74">&#34;qwe&#34;</span><span style="color:#960050;background-color:#1e0010">}</span>, <span style="color:#960050;background-color:#1e0010">{$</span>addToSet:<span style="color:#960050;background-color:#1e0010">{</span>hobby:<span style="color:#e6db74">&#34;java&#34;</span><span style="color:#960050;background-color:#1e0010">}}</span>) <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">若存在，不添加</span>
</span></span></code></pre></div><h4 id="删除元素">删除元素</h4>
<blockquote>
<p><strong>$pop &ndash;&gt; 类似弹栈</strong></p>
<blockquote>
<p><strong>1</strong> &ndash; 删除最后一个元素</p>
<p><strong>-1</strong> &ndash; 删除第一个元素</p>
</blockquote>
<p><strong>$pull &ndash;&gt; 删除指定的元素</strong></p>
</blockquote>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span>db.users.find(<span style="color:#960050;background-color:#1e0010">{</span>name:<span style="color:#e6db74">&#34;qwe&#34;</span><span style="color:#960050;background-color:#1e0010">}</span>, <span style="color:#960050;background-color:#1e0010">{$</span>pop:<span style="color:#960050;background-color:#1e0010">{</span>hobby:<span style="color:#ae81ff">1</span><span style="color:#960050;background-color:#1e0010">}}</span>) <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">删除最后一个元素</span>
</span></span><span style="display:flex;"><span>db.users.find(<span style="color:#960050;background-color:#1e0010">{</span>name:<span style="color:#e6db74">&#34;qwe&#34;</span><span style="color:#960050;background-color:#1e0010">}</span>, <span style="color:#960050;background-color:#1e0010">{$</span>pop:<span style="color:#960050;background-color:#1e0010">{</span>hobby:<span style="color:#f92672">-</span><span style="color:#ae81ff">1</span><span style="color:#960050;background-color:#1e0010">}}</span>) <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">删除第一个元素</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>db.users.find(<span style="color:#960050;background-color:#1e0010">{</span>name:<span style="color:#e6db74">&#34;qwe&#34;</span><span style="color:#960050;background-color:#1e0010">}</span>, <span style="color:#960050;background-color:#1e0010">{$</span>pull:<span style="color:#960050;background-color:#1e0010">{</span>hobby:<span style="color:#e6db74">&#34;c&#34;</span><span style="color:#960050;background-color:#1e0010">}}</span>) <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">删除数组中”</span><span style="color:#66d9ef">c</span><span style="color:#960050;background-color:#1e0010">“元素</span>
</span></span></code></pre></div><h4 id="修改元素">修改元素</h4>
<h5 id="通过索引修改">通过索引修改</h5>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span><span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">把用户名为</span>qwe的hobby的第三个元素修改为java
</span></span><span style="display:flex;"><span>db.users.<span style="color:#66d9ef">update</span>(<span style="color:#960050;background-color:#1e0010">{</span>name:<span style="color:#e6db74">&#34;qwe&#34;</span><span style="color:#960050;background-color:#1e0010">}</span>, <span style="color:#960050;background-color:#1e0010">{$</span><span style="color:#66d9ef">set</span>:<span style="color:#960050;background-color:#1e0010">{</span><span style="color:#e6db74">&#34;hobby.2&#34;</span>:<span style="color:#e6db74">&#34;java&#34;</span><span style="color:#960050;background-color:#1e0010">}}</span>)
</span></span></code></pre></div><h5 id="通过元素修改">通过元素修改</h5>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span><span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">把用户名为</span>qwe的hobby中的vue改为linux
</span></span><span style="display:flex;"><span>db.users.<span style="color:#66d9ef">update</span>(<span style="color:#960050;background-color:#1e0010">{</span>name:<span style="color:#e6db74">&#34;qwe&#34;</span>, hobby:<span style="color:#e6db74">&#34;vue&#34;</span><span style="color:#960050;background-color:#1e0010">}</span>, <span style="color:#960050;background-color:#1e0010">{$</span><span style="color:#66d9ef">set</span>:<span style="color:#960050;background-color:#1e0010">{</span><span style="color:#e6db74">&#34;hobby.$&#34;</span>:<span style="color:#e6db74">&#34;linux&#34;</span><span style="color:#960050;background-color:#1e0010">}}</span>)
</span></span></code></pre></div><blockquote>
<p><code>这里的**$**表示前面的**vue**</code></p>
</blockquote>
<h2 id="spring-data-mongodb">Spring Data MongoDB</h2>
<h3 id="准备环境">准备环境</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;parent&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;artifactId&gt;</span>spring-boot-starter-parent<span style="color:#f92672">&lt;/artifactId&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;groupId&gt;</span>org.springframework.boot<span style="color:#f92672">&lt;/groupId&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;version&gt;</span>2.1.3.RELEASE<span style="color:#f92672">&lt;/version&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/parent&gt;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;dependencies&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;dependency&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&lt;groupId&gt;</span>org.springframework.boot<span style="color:#f92672">&lt;/groupId&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&lt;artifactId&gt;</span>spring-boot-starter<span style="color:#f92672">&lt;/artifactId&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;/dependency&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;dependency&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&lt;groupId&gt;</span>org.springframework.boot<span style="color:#f92672">&lt;/groupId&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&lt;artifactId&gt;</span>spring-boot-starter-test<span style="color:#f92672">&lt;/artifactId&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;/dependency&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">&lt;!-- lombok --&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;dependency&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&lt;groupId&gt;</span>org.projectlombok<span style="color:#f92672">&lt;/groupId&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&lt;artifactId&gt;</span>lombok<span style="color:#f92672">&lt;/artifactId&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;/dependency&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">&lt;!-- Spring boot data mongodb --&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;dependency&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&lt;groupId&gt;</span>org.springframework.boot<span style="color:#f92672">&lt;/groupId&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&lt;artifactId&gt;</span>spring-boot-starter-data-mongodb<span style="color:#f92672">&lt;/artifactId&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;/dependency&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/dependencies&gt;</span>
</span></span></code></pre></div><h3 id="配置连接参数">配置连接参数</h3>
<pre tabindex="0"><code class="language-properties" data-lang="properties"># application.properties
# 配置数据库连接
spring.data.mongodb.uri=mongodb://127.0.0.1:27017/数据库名

# 配置MongoTemplate的执行日志
logging.level.org.springframework.data.mongodb.core=DEBUG
</code></pre><h3 id="domain">domain</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@Data</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">@AllArgsConstructor</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">@NoArgsConstructor</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">@Document</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;users&#34;</span><span style="color:#f92672">)</span> <span style="color:#75715e">//设置文档所在的集合
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Users</span> <span style="color:#f92672">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">@Id</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">private</span> ObjectId _id<span style="color:#f92672">;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">private</span> Long id<span style="color:#f92672">;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">private</span> String name<span style="color:#f92672">;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">private</span> Integer age<span style="color:#f92672">;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">}</span>
</span></span></code></pre></div><h3 id="mongorepository">MongoRepository</h3>
<p>该接口对MongoDB数据的常用操作进行了封装，我们只需要写个接口去继承该接口就能直接拥有CRUD等基本操作，再学习一下<!-- raw HTML omitted --><strong>JPA</strong>的方法命名规范<!-- raw HTML omitted -->，可以毫不费力的完成各种高级操作</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#75715e">/**
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"> * 自定义一个接口继承MongoRepository
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"> * 泛型1：domain类型
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"> * 泛型2：文档主键类型
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"> * 贴上@Repository注解，底层回创建出动态代理对象，交给Spring管理
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"> */</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">@Repository</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">interface</span> <span style="color:#a6e22e">UserMongoRepository</span> <span style="color:#66d9ef">extends</span> MongoRepository<span style="color:#f92672">&lt;</span>Users<span style="color:#f92672">,</span> ObjectId<span style="color:#f92672">&gt;</span> <span style="color:#f92672">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//使用Spring Data命名规范做高级查询
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    Users <span style="color:#a6e22e">findByName</span><span style="color:#f92672">(</span>String name<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">}</span>
</span></span></code></pre></div><h3 id="spring-data方法命名规范">Spring Data方法命名规范</h3>
<table>
<thead>
<tr>
<th style="text-align:center">关键字</th>
<th style="text-align:center">例子</th>
<th style="text-align:center">JPQL</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">And</td>
<td style="text-align:center">findByNameAndAge(String name, Integer age)</td>
<td style="text-align:center">where name=? and age=?</td>
</tr>
<tr>
<td style="text-align:center">Or</td>
<td style="text-align:center">findByNameOrAge(String name, String age)</td>
<td style="text-align:center">where name=? or age=?</td>
</tr>
<tr>
<td style="text-align:center">Is</td>
<td style="text-align:center">findByName(String name)</td>
<td style="text-align:center">where name=?</td>
</tr>
<tr>
<td style="text-align:center">Between</td>
<td style="text-align:center">findByAgeBetween(Integer min, Integer max)</td>
<td style="text-align:center">where age between ? and ?</td>
</tr>
<tr>
<td style="text-align:center">LessThan</td>
<td style="text-align:center">findByAgeLessThan(Integer age)</td>
<td style="text-align:center">where age&lt;?</td>
</tr>
<tr>
<td style="text-align:center">GreaterThan</td>
<td style="text-align:center">findByAgeGreaterThan(Integer age)</td>
<td style="text-align:center">where age&gt;?</td>
</tr>
<tr>
<td style="text-align:center">LessThanEqual</td>
<td style="text-align:center">findByAgeLessThanEqual(Integer age)</td>
<td style="text-align:center">where age&lt;=?</td>
</tr>
<tr>
<td style="text-align:center">GreaterThanEqual</td>
<td style="text-align:center">findByAgeGreaterThanEqual(Integer age)</td>
<td style="text-align:center">where age&gt;=?</td>
</tr>
<tr>
<td style="text-align:center">After</td>
<td style="text-align:center">等同于GreaterThan</td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center">Before</td>
<td style="text-align:center">等同于LessThan</td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center">IsNull</td>
<td style="text-align:center">findByNameIsNull()</td>
<td style="text-align:center">where name is null</td>
</tr>
<tr>
<td style="text-align:center">IsNotNull</td>
<td style="text-align:center">findByNameIsNotNull()</td>
<td style="text-align:center">where name is not null</td>
</tr>
<tr>
<td style="text-align:center">Like</td>
<td style="text-align:center">findByNameLike(String name)</td>
<td style="text-align:center">where name like ?</td>
</tr>
<tr>
<td style="text-align:center">NotLike</td>
<td style="text-align:center">findByNameNotLike(String name)</td>
<td style="text-align:center">where name not like ?</td>
</tr>
<tr>
<td style="text-align:center">StartingWith</td>
<td style="text-align:center">findByNameStartingWith(String name)</td>
<td style="text-align:center">where name like &lsquo;?%&rsquo;</td>
</tr>
<tr>
<td style="text-align:center">EndingWith</td>
<td style="text-align:center">findByNameEndingWith(String name)</td>
<td style="text-align:center">where name like &lsquo;%?&rsquo;</td>
</tr>
<tr>
<td style="text-align:center">Containing</td>
<td style="text-align:center">findByNameContaining(String name)</td>
<td style="text-align:center">where name like &lsquo;%?%&rsquo;</td>
</tr>
<tr>
<td style="text-align:center">OrderByXx[desc]</td>
<td style="text-align:center">findByIdOrderByXx[Desc](Long id)</td>
<td style="text-align:center">where id=? order by Xx [Desc]</td>
</tr>
<tr>
<td style="text-align:center">Not</td>
<td style="text-align:center">findByNameNot(String name)</td>
<td style="text-align:center">where name !=?</td>
</tr>
<tr>
<td style="text-align:center">In</td>
<td style="text-align:center">findByIdIn(List<!-- raw HTML omitted --> ids)</td>
<td style="text-align:center">where id in (&hellip;)</td>
</tr>
<tr>
<td style="text-align:center">NotIn</td>
<td style="text-align:center">findByIdNotIn(List<!-- raw HTML omitted --> ids)</td>
<td style="text-align:center">where id not in (&hellip;)</td>
</tr>
<tr>
<td style="text-align:center">True</td>
<td style="text-align:center">findByXxTrue()</td>
<td style="text-align:center">where Xx = true</td>
</tr>
<tr>
<td style="text-align:center">False</td>
<td style="text-align:center">findByXxFalse()</td>
<td style="text-align:center">where Xx = false</td>
</tr>
<tr>
<td style="text-align:center">IgnoreCase</td>
<td style="text-align:center">findByNameIgnoreCase(String name)</td>
<td style="text-align:center">where name=?（忽略大小写）</td>
</tr>
</tbody>
</table>
<h3 id="test">Test</h3>
<p>在测试类中注入<code>UserMongoRepository</code></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@Autowired</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">private</span> UserMongoRepository repository<span style="color:#f92672">;</span>
</span></span></code></pre></div><ul>
<li>
<p><strong>插入/更新一个文档</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-Java" data-lang="Java"><span style="display:flex;"><span><span style="color:#a6e22e">@Test</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">testSaveOrUpdate</span><span style="color:#f92672">()</span> <span style="color:#f92672">{</span>
</span></span><span style="display:flex;"><span>    Users users <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> Users<span style="color:#f92672">();</span>
</span></span><span style="display:flex;"><span>    users<span style="color:#f92672">.</span><span style="color:#a6e22e">set_id</span><span style="color:#f92672">(</span><span style="color:#66d9ef">null</span><span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>    users<span style="color:#f92672">.</span><span style="color:#a6e22e">setId</span><span style="color:#f92672">(</span>5L<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>    users<span style="color:#f92672">.</span><span style="color:#a6e22e">setName</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;珊迪&#34;</span><span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>    users<span style="color:#f92672">.</span><span style="color:#a6e22e">setAge</span><span style="color:#f92672">(</span>18<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>    repository<span style="color:#f92672">.</span><span style="color:#a6e22e">save</span><span style="color:#f92672">(</span>users<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">}</span>
</span></span></code></pre></div><p>日志：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">Inserting</span> <span style="color:#960050;background-color:#1e0010">Document</span> <span style="color:#960050;background-color:#1e0010">containing</span> <span style="color:#960050;background-color:#1e0010">fields:</span> 
</span></span><span style="display:flex;"><span>[<span style="color:#960050;background-color:#1e0010">id</span>, <span style="color:#960050;background-color:#1e0010">name</span>, <span style="color:#960050;background-color:#1e0010">age</span>, <span style="color:#960050;background-color:#1e0010">_class</span>] <span style="color:#960050;background-color:#1e0010">in</span> <span style="color:#960050;background-color:#1e0010">collection:</span> <span style="color:#960050;background-color:#1e0010">users</span>
</span></span></code></pre></div></li>
<li>
<p><strong>删除一个文档</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@Test</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">testDelete</span><span style="color:#f92672">()</span> <span style="color:#f92672">{</span>
</span></span><span style="display:flex;"><span>    repository<span style="color:#f92672">.</span><span style="color:#a6e22e">deleteById</span><span style="color:#f92672">(</span><span style="color:#66d9ef">new</span> ObjectId<span style="color:#f92672">(</span><span style="color:#e6db74">&#34;63b05590920e000023000c27&#34;</span><span style="color:#f92672">));</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">}</span>
</span></span></code></pre></div><p>日志：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">Remove</span> <span style="color:#960050;background-color:#1e0010">using</span> <span style="color:#960050;background-color:#1e0010">query:</span>
</span></span><span style="display:flex;"><span>{ <span style="color:#f92672">&#34;_id&#34;</span> : { <span style="color:#f92672">&#34;$oid&#34;</span> : <span style="color:#e6db74">&#34;63b05590920e000023000c27&#34;</span> } } 
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">in</span> <span style="color:#960050;background-color:#1e0010">collection:</span> <span style="color:#960050;background-color:#1e0010">users</span>
</span></span></code></pre></div></li>
<li>
<p><strong>查询一个文档</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@Test</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">testGet</span><span style="color:#f92672">()</span> <span style="color:#f92672">{</span>
</span></span><span style="display:flex;"><span>    Optional<span style="color:#f92672">&lt;</span>Users<span style="color:#f92672">&gt;</span> optional <span style="color:#f92672">=</span> repository<span style="color:#f92672">.</span><span style="color:#a6e22e">findById</span><span style="color:#f92672">(</span><span style="color:#66d9ef">new</span> ObjectId<span style="color:#f92672">(</span><span style="color:#e6db74">&#34;63b05590920e000023000c25&#34;</span><span style="color:#f92672">));</span>
</span></span><span style="display:flex;"><span>    optional<span style="color:#f92672">.</span><span style="color:#a6e22e">ifPresent</span><span style="color:#f92672">(</span>System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span><span style="color:#f92672">::</span>println<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">}</span>
</span></span></code></pre></div><p>日志：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">findOne</span> <span style="color:#960050;background-color:#1e0010">using</span> <span style="color:#960050;background-color:#1e0010">query:</span> 
</span></span><span style="display:flex;"><span>{ <span style="color:#f92672">&#34;_id&#34;</span> : { <span style="color:#f92672">&#34;$oid&#34;</span> : <span style="color:#e6db74">&#34;63b05590920e000023000c25&#34;</span> } } <span style="color:#960050;background-color:#1e0010">fields:</span> { } <span style="color:#960050;background-color:#1e0010">in</span> <span style="color:#960050;background-color:#1e0010">db.collection:</span> <span style="color:#960050;background-color:#1e0010">czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">.users</span>
</span></span></code></pre></div></li>
<li>
<p><strong>根据姓名查询一个文档</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@Test</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">testByName</span><span style="color:#f92672">()</span> <span style="color:#f92672">{</span>
</span></span><span style="display:flex;"><span>    Users u <span style="color:#f92672">=</span> repository<span style="color:#f92672">.</span><span style="color:#a6e22e">findByName</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;海绵宝宝&#34;</span><span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>    System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span><span style="color:#f92672">.</span><span style="color:#a6e22e">println</span><span style="color:#f92672">(</span>u<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">}</span>
</span></span></code></pre></div><p>日志：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">find</span> <span style="color:#960050;background-color:#1e0010">using</span> <span style="color:#960050;background-color:#1e0010">query:</span>
</span></span><span style="display:flex;"><span>{ <span style="color:#f92672">&#34;name&#34;</span> : <span style="color:#e6db74">&#34;海绵宝宝&#34;</span> } <span style="color:#960050;background-color:#1e0010">fields:</span> <span style="color:#960050;background-color:#1e0010">Document</span>{<span style="color:#960050;background-color:#1e0010">{</span>}<span style="color:#960050;background-color:#1e0010">}</span> 
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">for</span> <span style="color:#960050;background-color:#1e0010">class:</span> <span style="color:#960050;background-color:#1e0010">class</span> <span style="color:#960050;background-color:#1e0010">cn.czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">.mongodb.domain.Users</span> <span style="color:#960050;background-color:#1e0010">in</span> <span style="color:#960050;background-color:#1e0010">collection:</span> <span style="color:#960050;background-color:#1e0010">users</span>
</span></span></code></pre></div></li>
<li>
<p><strong>查询所有文档</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@Test</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">testList</span><span style="color:#f92672">()</span> <span style="color:#f92672">{</span>
</span></span><span style="display:flex;"><span>    List<span style="color:#f92672">&lt;</span>Users<span style="color:#f92672">&gt;</span> list <span style="color:#f92672">=</span> repository<span style="color:#f92672">.</span><span style="color:#a6e22e">findAll</span><span style="color:#f92672">();</span>
</span></span><span style="display:flex;"><span>    list<span style="color:#f92672">.</span><span style="color:#a6e22e">forEach</span><span style="color:#f92672">(</span>System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span><span style="color:#f92672">::</span>println<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">}</span>
</span></span></code></pre></div><p>日志：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">find</span> <span style="color:#960050;background-color:#1e0010">using</span> <span style="color:#960050;background-color:#1e0010">query:</span> 
</span></span><span style="display:flex;"><span>{ } <span style="color:#960050;background-color:#1e0010">fields:</span> <span style="color:#960050;background-color:#1e0010">Document</span>{<span style="color:#960050;background-color:#1e0010">{</span>}<span style="color:#960050;background-color:#1e0010">}</span> 
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">for</span> <span style="color:#960050;background-color:#1e0010">class:</span> <span style="color:#960050;background-color:#1e0010">class</span> <span style="color:#960050;background-color:#1e0010">cn.czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">.mongodb.domain.Users</span> <span style="color:#960050;background-color:#1e0010">in</span> <span style="color:#960050;background-color:#1e0010">collection:</span> <span style="color:#960050;background-color:#1e0010">users</span>
</span></span></code></pre></div></li>
</ul>
<h3 id="mongotemplate">MongoTemplate</h3>
<p>该对象有Spring Boot完成了自动装配，存入Spring容器中，我们直接注入就可以使用了，依靠该对象能完成任何MongoDB的操作，一般和MongoRepository分工合作，多数用于复杂的高级查询以及底层操作</p>
<h4 id="条件查询">条件查询</h4>
<p>Query对象用于封装查询条件，配合Criteria一起使用，来完成各种条件的描述</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#75715e">//一个Criteria对象可以理解为是一个限定条件
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Criteria<span style="color:#f92672">.</span><span style="color:#a6e22e">where</span><span style="color:#f92672">(</span>String key<span style="color:#f92672">).</span><span style="color:#a6e22e">is</span><span style="color:#f92672">(</span>Object val<span style="color:#f92672">);</span> <span style="color:#75715e">//设置一个等值条件
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Criteria<span style="color:#f92672">.</span><span style="color:#a6e22e">orOperator</span><span style="color:#f92672">(</span>Criteria <span style="color:#f92672">...);</span> <span style="color:#75715e">//设置一组或的逻辑条件
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//模糊查询
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Criteria<span style="color:#f92672">.</span><span style="color:#a6e22e">where</span><span style="color:#f92672">(</span>String key<span style="color:#f92672">).</span><span style="color:#a6e22e">regex</span><span style="color:#f92672">(</span>String regex<span style="color:#f92672">);</span> <span style="color:#75715e">//使用正则表达式匹配查询
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>注意<span style="color:#960050;background-color:#1e0010">：</span>Criteria对象可以由其静态方法和构造器获取
</span></span><span style="display:flex;"><span>Criteria封装了所有对条件的描述<span style="color:#960050;background-color:#1e0010">，</span>常见的方法有<span style="color:#960050;background-color:#1e0010">：</span>
</span></span><span style="display:flex;"><span>lt <span style="color:#f92672">/</span> lte <span style="color:#f92672">/</span> gt <span style="color:#f92672">/</span> gte <span style="color:#f92672">/</span> ne <span style="color:#f92672">/</span> <span style="color:#f92672">...</span>
</span></span></code></pre></div><p>最后通过Query对象的addCriteria把条件封装到Query对象中</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>Query对象<span style="color:#f92672">.</span><span style="color:#a6e22e">addCriteria</span><span style="color:#f92672">(</span>Criteria criteria<span style="color:#f92672">);</span> <span style="color:#75715e">//添加查询条件
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Query对象<span style="color:#f92672">.</span><span style="color:#a6e22e">skip</span><span style="color:#f92672">(</span>start<span style="color:#f92672">).</span><span style="color:#a6e22e">limit</span><span style="color:#f92672">(</span>pageSize<span style="color:#f92672">);</span> <span style="color:#75715e">//分页查询
</span></span></span></code></pre></div><h4 id="api方法">API方法</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#75715e">//根据条件查询集合中的文档
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>List<span style="color:#f92672">&lt;</span>T<span style="color:#f92672">&gt;</span> mongoTemplate<span style="color:#f92672">.</span><span style="color:#a6e22e">find</span><span style="color:#f92672">(</span>Query query<span style="color:#f92672">,</span> Class<span style="color:#f92672">&lt;</span>T<span style="color:#f92672">&gt;</span> type<span style="color:#f92672">,</span> String collectionName<span style="color:#f92672">);</span>
</span></span></code></pre></div><h4 id="api使用">API使用</h4>
<p>在测试类中注入<code>MongoTemplate</code></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@Autowired</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">private</span> MongoTemplate template<span style="color:#f92672">;</span>
</span></span></code></pre></div><ul>
<li>
<p><strong>分页查询文档，显示第二页，每页显示2条数据，按照id升序排列</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@Test</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">testQuery1</span><span style="color:#f92672">()</span> <span style="color:#f92672">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//创建查询对象
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    Query query <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> Query<span style="color:#f92672">();</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//设置分页信息
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    query<span style="color:#f92672">.</span><span style="color:#a6e22e">skip</span><span style="color:#f92672">(</span>2<span style="color:#f92672">).</span><span style="color:#a6e22e">limit</span><span style="color:#f92672">(</span>2<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//设置排序规则
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    query<span style="color:#f92672">.</span><span style="color:#a6e22e">with</span><span style="color:#f92672">(</span><span style="color:#66d9ef">new</span> Sort<span style="color:#f92672">(</span>Sort<span style="color:#f92672">.</span><span style="color:#a6e22e">Direction</span><span style="color:#f92672">.</span><span style="color:#a6e22e">ASC</span><span style="color:#f92672">,</span> <span style="color:#e6db74">&#34;id&#34;</span><span style="color:#f92672">));</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    List<span style="color:#f92672">&lt;</span>Users<span style="color:#f92672">&gt;</span> users <span style="color:#f92672">=</span> template<span style="color:#f92672">.</span><span style="color:#a6e22e">find</span><span style="color:#f92672">(</span>query<span style="color:#f92672">,</span> Users<span style="color:#f92672">.</span><span style="color:#a6e22e">class</span><span style="color:#f92672">,</span> <span style="color:#e6db74">&#34;users&#34;</span><span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>    users<span style="color:#f92672">.</span><span style="color:#a6e22e">forEach</span><span style="color:#f92672">(</span>System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span><span style="color:#f92672">::</span>println<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">}</span>
</span></span></code></pre></div></li>
<li>
<p><strong>查询所有name为派大星或者age&lt;20的文档</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@Test</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">testQuery2</span><span style="color:#f92672">()</span> <span style="color:#f92672">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//构建限制条件 {&#34;$or&#34;: [{&#34;name&#34;: &#34;派大星&#34;}, {&#34;age&#34;: {&#34;$lt&#34;:20}}]}
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    Criteria criteria <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> Criteria<span style="color:#f92672">().</span><span style="color:#a6e22e">orOperator</span><span style="color:#f92672">(</span>
</span></span><span style="display:flex;"><span>            Criteria<span style="color:#f92672">.</span><span style="color:#a6e22e">where</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;name&#34;</span><span style="color:#f92672">).</span><span style="color:#a6e22e">is</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;派大星&#34;</span><span style="color:#f92672">),</span>
</span></span><span style="display:flex;"><span>            Criteria<span style="color:#f92672">.</span><span style="color:#a6e22e">where</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;age&#34;</span><span style="color:#f92672">).</span><span style="color:#a6e22e">lt</span><span style="color:#f92672">(</span>20<span style="color:#f92672">)</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//创建查询对象
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    Query query <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> Query<span style="color:#f92672">();</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//添加限制条件
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    query<span style="color:#f92672">.</span><span style="color:#a6e22e">addCriteria</span><span style="color:#f92672">(</span>criteria<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>    List<span style="color:#f92672">&lt;</span>Users<span style="color:#f92672">&gt;</span> list <span style="color:#f92672">=</span> template<span style="color:#f92672">.</span><span style="color:#a6e22e">find</span><span style="color:#f92672">(</span>query<span style="color:#f92672">,</span> Users<span style="color:#f92672">.</span><span style="color:#a6e22e">class</span><span style="color:#f92672">,</span> <span style="color:#e6db74">&#34;users&#34;</span><span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>    list<span style="color:#f92672">.</span><span style="color:#a6e22e">forEach</span><span style="color:#f92672">(</span>System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span><span style="color:#f92672">::</span>println<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">}</span>
</span></span></code></pre></div></li>
<li>
<p><strong>查询所有name含有&quot;海&quot;并且 1&lt;=age&lt;=20的文档</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@Test</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">testQuery3</span><span style="color:#f92672">()</span> <span style="color:#f92672">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//构建查询条件
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#75715e">// db.users.find({&#34;$and&#34;: [{&#34;name&#34;: {&#34;$regex&#34;: &#34;.*海.*&#34;}},{&#34;age&#34;: {$gte&#34;: 1,&#34;$lte&#34;: 20}}]})
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    Criteria criteria <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> Criteria<span style="color:#f92672">().</span><span style="color:#a6e22e">andOperator</span><span style="color:#f92672">(</span>
</span></span><span style="display:flex;"><span>            Criteria<span style="color:#f92672">.</span><span style="color:#a6e22e">where</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;name&#34;</span><span style="color:#f92672">).</span><span style="color:#a6e22e">regex</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;.*海.*&#34;</span><span style="color:#f92672">),</span>
</span></span><span style="display:flex;"><span>            Criteria<span style="color:#f92672">.</span><span style="color:#a6e22e">where</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;age&#34;</span><span style="color:#f92672">).</span><span style="color:#a6e22e">gte</span><span style="color:#f92672">(</span>1<span style="color:#f92672">).</span><span style="color:#a6e22e">lte</span><span style="color:#f92672">(</span>20<span style="color:#f92672">)</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//创建查询对象
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    Query query <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> Query<span style="color:#f92672">();</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//添加限制条件
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    query<span style="color:#f92672">.</span><span style="color:#a6e22e">addCriteria</span><span style="color:#f92672">(</span>criteria<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>    List<span style="color:#f92672">&lt;</span>Users<span style="color:#f92672">&gt;</span> list <span style="color:#f92672">=</span> template<span style="color:#f92672">.</span><span style="color:#a6e22e">find</span><span style="color:#f92672">(</span>query<span style="color:#f92672">,</span> Users<span style="color:#f92672">.</span><span style="color:#a6e22e">class</span><span style="color:#f92672">,</span> <span style="color:#e6db74">&#34;users&#34;</span><span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>    list<span style="color:#f92672">.</span><span style="color:#a6e22e">forEach</span><span style="color:#f92672">(</span>System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span><span style="color:#f92672">::</span>println<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">}</span>
</span></span></code></pre></div></li>
</ul>

    <h4><a href="https://czh-dev.gitee.io/czh-blog.gitee.io/">Back to Home</a></h4>
  </div>

  <div class="span3 bs-docs-sidebar" style="position:fixed;right: 40px;top: 50px;">
    <h1>catalogue</h1>
    <ul class="nav nav-list bs-docs-sidenav">
      <div class="toc-div">
        <nav id="TableOfContents">
  <ul>
    <li><a href="#mongodb介绍">MongoDB介绍</a>
      <ul>
        <li><a href="#简介">简介</a></li>
        <li><a href="#特点">特点</a></li>
        <li><a href="#对比mysql">对比MySQL</a></li>
        <li><a href="#数据库结构">数据库结构</a></li>
        <li><a href="#语法">语法</a></li>
        <li><a href="#数据类型">数据类型</a></li>
      </ul>
    </li>
    <li><a href="#安装和连接">安装和连接</a></li>
    <li><a href="#创建操作">创建操作</a></li>
    <li><a href="#删除操作">删除操作</a></li>
    <li><a href="#更新操作">更新操作</a></li>
    <li><a href="#读取操作">读取操作</a>
      <ul>
        <li><a href="#正常查询">正常查询</a></li>
        <li><a href="#分页查询">分页查询</a></li>
        <li><a href="#排序">排序</a></li>
        <li><a href="#高级查询">高级查询</a></li>
        <li><a href="#数组----push">数组 &ndash; $push</a></li>
      </ul>
    </li>
    <li><a href="#spring-data-mongodb">Spring Data MongoDB</a>
      <ul>
        <li><a href="#准备环境">准备环境</a></li>
        <li><a href="#配置连接参数">配置连接参数</a></li>
        <li><a href="#domain">domain</a></li>
        <li><a href="#mongorepository">MongoRepository</a></li>
        <li><a href="#spring-data方法命名规范">Spring Data方法命名规范</a></li>
        <li><a href="#test">Test</a></li>
        <li><a href="#mongotemplate">MongoTemplate</a></li>
      </ul>
    </li>
  </ul>
</nav>
      </div>
    </ul>
  </div>

</div>
<script src="https://cdn.jsdelivr.net/npm/gumshoejs@5.1.2/dist/gumshoe.min.js"></script>
<script>
  var spy = new Gumshoe('#TableOfContents a', {
    nested: true,
    nestedClass: 'active'
  });
</script>
<style>
   
  #TableOfContents li,
  #TableOfContents ul {
    list-style-type: none;
  }

  #TableOfContents ul {
    padding-left: 0px;
  }

  #TableOfContents li>a {
    display: block;
    padding: 4px 20px;
    font-size: 95%;
    color: #000000;
  }

  #TableOfContents li>a:hover,
  #TableOfContents li>a:focus {
    padding-left: 19px;
    color: #3A6bA5;
    text-decoration: none;
    background-color: transparent;
    border-left: 1px solid #3A6bA5;
  }

  #TableOfContents li.active>a,
  #TableOfContents li.active>a:hover,
  #TableOfContents li.active>a:focus {
    padding-left: 18px;
    font-weight: bold;
    color: #3A6bA5;
    background-color: transparent;
    border-left: 2px solid #3A6bA5;
  }

   
  #TableOfContents li>ul {
    padding-bottom: 10px;
  }

  #TableOfContents li li>a {
    padding-top: 1px;
    padding-bottom: 1px;
    padding-left: 30px;
    font-size: 14px;
    font-weight: normal;
  }

  #TableOfContents li li>a:hover,
  #TableOfContents li li>a:focus {
    padding-left: 29px;
  }

  #TableOfContents li li.active>a,
  #TableOfContents li li.active>a:hover,
  #TableOfContents li li.active>a:focus {
    padding-left: 28px;
    font-weight: 500;
  }

  #TableOfContents .nav-link.active+ul {
    display: block;
  }

  #TableOfContents li>ul {
    display: none;
  }

  #TableOfContents li.active>ul {
    display: inherit;
  }

  .toc-div {
    position: -webkit-sticky;
     
    position: sticky;
     
    top: 20px;
  }
</style>


        </div><footer class="container">
    <hr class="soften">
    <p>
    <a href="https://space.bilibili.com/1799809923">Love eating fried pork ribs</a> | 

&copy; 
<a href="http://jmf-portfolio.netlify.com" target="_blank">
    JM Fergeau
</a>
<span id="thisyear">2023</span>

    | My site


        | Built on <a href="//gohugo.io" target="_blank">Hugo</a>

</p>
    <p class="text-center">
        <a href="https://facebook.com">Facebook</a> 
        <a href="https://twitter.com">Twitter</a> 
        <a href="https://linkedin.com">Linkedin</a> 
        <a href="https://github.com">GitHub</a> 
        <a href="https://gitlab.com">GitLab</a>
    </p>
</footer>

</body><link rel="stylesheet" href="/czh-blog.gitee.io/css/bootstrap.css">
<link rel="stylesheet" href="/czh-blog.gitee.io/css/bootstrap-responsive.css">
<link rel="stylesheet" href="/czh-blog.gitee.io/css/style.css">

<script src="/czh-blog.gitee.io/js/jquery.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-386.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-transition.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-alert.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-modal.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-dropdown.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-scrollspy.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-tab.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-tooltip.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-popover.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-button.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-collapse.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-carousel.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-typeahead.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-affix.js"></script>
<script>
    _386 = { 
        fastLoad: false ,
        onePass: false , 
        speedFactor: 1 
    };

    
    function ThisYear() {
        document.getElementById('thisyear').innerHTML = new Date().getFullYear();
    };
</script>
</html>
